package NWish
{
	import NWish.Interface.IApp;
	import NWish.Interface.ILogger;
	import NWish.utils.ErrorManger;
	
	import away3d.core.base.Object3D;
	
	import flash.events.Event;
	import flash.net.SharedObject;
	
	public class Logger extends ILogger
	{
		private var _app:IApp;

		private var _window:Window;
		
		public function Logger(app:IApp)
		{
			//
			_app = app;
			
			//
			//AsWingManager.initAsStandard(_app.rootSprite);
			
			//
			_window = new Window();
			//_app.rootSprite.addChild(_window);
			
			_app.rootSprite.stage.addEventListener(Event.RESIZE, _onStageResize);
			
			//
			_app.rootSprite.addEventListener(NWEvent.CREATE_OBJECT,	onCreateObject);
			_app.rootSprite.addEventListener(NWEvent.DELETE_OBJECT,	onDeleteObject);
			//_app.addEventListener(NWEvent.SELSCT_OBJECT,	onSelectObject);
			_app.rootSprite.addEventListener(NWEvent.NEW_SCENE,		onNewScene);
			
			
			//
			_logger = this;
		}
		
		public function init()
		{
			//
			var menuBar:MenuBar = _app.getPlugin('MenuBar') as MenuBar;
			if(menuBar)
				menuBar.windowMenu.addMenuItem("&Logger").addActionListener(_showWindow);
			
			//
			_window.show();
		}
		
		private function _showWindow(e)
		{
			_window.show();
		}
		
		override protected function log(msg:String,obj,level)
		{
			if(level & _level)
			{
				if(obj)
				{
					var n = '';
					try
					{
						n += ' ' + obj.name;
					}
					catch(e)
					{
						
					}
					
					var ot:String = obj + '';
					ot = ot.replace('object ','');
					
					ot = ot.replace(']', n + ']');
					
					var l = '';
					if(level == WARN)
						l = 'warn';
					else if(level == ERR)
						l = 'err';
					
					var str = l + '> ' + ot + ' : ' + msg
					trace(str);
					_window.log(str);
					
					if(level == ERR)
						ErrorManger.saveErr(str);
				}
				else
				{
					trace(msg);
					_window.log(msg);
				}
			}
		}
		
		
		private function _onStageResize(event:Event) : void
		{
			_window.y = _app.rootSprite.stage.stageHeight - _window.height;
			//_window.height = _app.stage.stageHeight;
		}
		
		
		//
		private function getNames(objs)
		{
			var msg:String = '  ';
			for each(var o in objs)
			{
				msg += Misc.getObjectName(o);
			}
			return msg;
		}
		private function onCreateObject(e:NWEvent)
		{
			var objs = e.objects;
			if(objs is Array == false)
				objs = [objs];
			
			log('ceate object:' + getNames(objs),null,LOG);
		}
		private function onDeleteObject(e:NWEvent)
		{
			var objs = e.objects;
			if(objs is Array == false)
				objs = [objs];
				
			log('delete object:' + getNames(objs),null,LOG);
		}
		/*private function onSelectObject(e:NWEvent)
		{
			var objs = e.objects;
			if(objs is Array == false)
				objs = [objs];
		
			log('pick up object:' + getNames(objs));
		}*/
		private function onNewScene(e:NWEvent)
		{
			log('new scene!',null,LOG);
		}
		
	}
}



import NWish.NWEvent;

import org.aswing.*;
import org.aswing.border.*;
import org.aswing.colorchooser.*;
import org.aswing.event.AWEvent;
import org.aswing.ext.*;
import org.aswing.geom.*;

class Window extends JFrame
{
	private var _textArea:JTextArea;
	
	
	public function Window()
	{
		//
		super(null,'Logger');
		
		//
		setLocationXY(0,15);
		setSize(new IntDimension(350, 150));
		
		//
		var panel:JPanel = new JPanel();
		//panel.setSize(new IntDimension(200, 100));
		setContentPane(panel);
		
		//component creation
		var layout0:EmptyLayout = new EmptyLayout();
		panel.setLayout(layout0);
		
		_textArea = new JTextArea();
		_textArea.setLocation(new IntPoint(0, 0));
		_textArea.setSize(new IntDimension(350, 120));
		_textArea.setEditable(false);
		
		panel.append(_textArea);
	}
	
	public function log(msg)
	{
		_textArea.appendText(msg + '\n');
		_textArea.scrollToBottomLeft();
	}
	
}


